package mobile

import (
	"fmt"
	"strings"
	"tcsc/models"
	"tcsc/sword"
)

type Active struct {
	BaseController
}

func (c *Active) Get() {
	gbuyWhere := ""
	if c.ShopId != "" {
		gbuyWhere = "a.gid = " + c.ShopId + " and "
	} else {
		gbuyWhere = "a.gid=0 and "
	}
	nowtime := sword.Gettimes("")
	gbuy := models.Query(`select a.id,a.title,a.price,a.pid from mt_active_gbuy a where ` + gbuyWhere + ` a.status=1 and a.end=0 and 
		a.starttime<='` + nowtime + `' and a.endtime>'` + nowtime + `' order by 
		a.sort desc,a.addtime desc limit 3`)
	if gbuy != nil {
		for _, v := range gbuy {
			order := models.Query(`select count(a.id) as jnum from mt_order a where a.type=1 and a.status>=2 and a.agid=` + v["id"].(string))[0]
			v["jnum"] = order["jnum"]
			pid, ok := v["pid"].(string)
			if ok {
				v["product"] = ProductList(`select a.id,a.gid,a.title,b.pathname,a.keywords,a.price,a.isvip 
					from mt_product a 
					left join mt_resources b on a.img=b.id 
					where a.id=`+pid, c.Uid)[0]
			}
		}
	}
	ad := models.Query(`select a.url,b.pathname from mt_ad a left join mt_resources b on a.img=b.id where a.type=1 and a.status=1 
	and a.channel=65 order by a.sort desc`)
	if c.IsAjax() {
		var js = make(map[string]interface{})
		js["ad"] = ad
		js["gbuy"] = gbuy
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		c.Data["ad"] = ad
		c.Data["gbuy"] = gbuy
		c.TplName = "wechat/active/index.html"
	}
}

type ActiveGbuy struct {
	BaseController
}

func (c *ActiveGbuy) Get() {
	gbuyWhere := ""
	if c.ShopId != "" {
		gbuyWhere = "a.gid = " + c.ShopId + " and"
	} else {
		gbuyWhere = "a.gid=0 and"
	}
	var w []string
	nowtime := sword.Gettimes("")
	w = append(w, `a.starttime<='`+nowtime+`'`)
	w = append(w, `a.endtime>'`+nowtime+`'`)
	w = append(w, `a.status=1`)
	w = append(w, `a.end=0`)
	page, _, num := models.Pagechange(c.GetString("page"), "")
	lt := ` limit ` + page + `,` + num
	ob := ` order by a.sort desc,a.addtime desc`
	ws := strings.Join(w, ` and `)
	gbuy := models.Query(`select a.id,a.title,a.price,a.pid from mt_active_gbuy a where ` + gbuyWhere + ` ` + ws + ob + lt)
	if gbuy != nil {
		for _, v := range gbuy {
			order := models.Query(`select count(a.id) as jnum from mt_order a where a.type=1 and a.status>=2 and a.agid=` + v["id"].(string))[0]
			v["jnum"] = order["jnum"]
			pid, ok := v["pid"].(string)
			if ok {
				v["product"] = ProductList(`select a.id,a.gid,a.title,b.pathname,a.keywords,a.price,a.isvip 
					from mt_product a 
					left join mt_resources b on a.img=b.id 
					where a.id=`+pid, c.Uid)[0]
			}
		}
	}
	ad := models.Query(`select a.url,b.pathname from mt_ad a left join mt_resources b on a.img=b.id where a.type=1 and a.status=1 and a.platform=32 
	and a.channel=66 order by a.sort desc`)
	if c.IsAjax() {
		var js = make(map[string]interface{})
		js["ad"] = ad
		js["gbuy"] = gbuy
		c.Data["json"] = js
		c.ServeJSON()
	} else {
		c.Data["ad"] = ad
		c.Data["gbuy"] = gbuy
		c.TplName = "wechat/active/gbuy.html"
	}
}

type ActiveGbuyHelp struct {
	BaseController
}

func (c *ActiveGbuyHelp) Get() {
	help := models.Query(`select id,name,content from mt_page where cid=292 and status=1 order by sort desc,addtime desc limit 1`)
	for _, v := range help {
		v["content"] = sword.UeditorChange(models.Urlc, fmt.Sprint(v["content"]))
	}
	if help == nil {
		return
	}
	if c.IsAjax() {
		c.Data["json"] = help[0]
		c.ServeJSON()
	} else {
		c.Data["info"] = help[0]
		c.TplName = "wechat/active/gbuy_help.html"
	}
}
